package com.hsytech.dijiebao.photos.adapters;

import java.util.ArrayList;
import java.util.List;

import com.hsytech.dijiebao.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class PubSelectedImgsAdapter extends BaseAdapter {
	Context context;
	List<String> list;
	List<String> postList = new ArrayList<String>();
	OnItemClickClass onItemClickClass;
	int myposition;

	public PubSelectedImgsAdapter(Context context, List<String> data, OnItemClickClass onItemClickClass) {
		this.context = context;
		// if(data.size() >5 ){
		//
		// for(int i = 4 ; i < data.size() ; i++){
		// data.remove(i);
		// }
		// }
		this.list = data;
		this.onItemClickClass = onItemClickClass;

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		// Holder holder;
		myposition = position;
		// if (view == null) {
		view = LayoutInflater.from(context).inflate(R.layout.pub_selected_imgs_item, null);
		// final holder = new Holder();
		final ImageView imageView = (ImageView) view.findViewById(R.id.imageView);

		ImageView delete_img = (ImageView) view.findViewById(R.id.delete_img);
		// delete_img.setVisibility(View.GONE);

		// view.setTag(holder);
		// } else {
		// holder = (Holder) view.getTag();
		// }

		if (list.get(myposition) == "a") {

			imageView.setVisibility(View.VISIBLE);
			delete_img.setVisibility(View.GONE);
		} else {

			ImageLoader.getInstance().displayImage("file://" + list.get(position), imageView);
			delete_img.setOnClickListener(new OnPhotoClick(list.get(position), position));
		}
		return view;
	}

	public List<String> getPostList() {

		return list;
	}

	public interface OnItemClickClass {
		public void OnItemClick(View v, String filepath, int delPosition);
	}

	class Holder {
		ImageView imageView, delete_img;
	}

	class OnPhotoClick implements OnClickListener {
		String filepath;
		int mPosition;

		public OnPhotoClick(String filepath, int delPosition) {
			this.filepath = filepath;
			this.mPosition = delPosition;
		}

		@Override
		public void onClick(View v) {
			if (list != null && onItemClickClass != null) {
				onItemClickClass.OnItemClick(v, filepath,mPosition);
			}

		}
	}
}
